package com.ww.java7.tryauto;

import java.io.IOException;

/**
 * @author wanggw
 * @Date 2023年03月01 0:07
 */
public class AutoCloseThrow {

    /**
     * 资源读取
     * 资源关闭
     * Exception in thread "main" java.io.IOException: 读取异常
     * 	at com.ww.java.java7.tryauto.FileReadAutoClose.read(AutoCloseThrow.java:22)
     * 	at com.ww.java.java7.tryauto.AutoCloseThrow.main(AutoCloseThrow.java:13)
     * 	Suppressed: java.io.IOException: 关闭异常
     * 		at com.ww.java.java7.tryauto.FileReadAutoClose.close(AutoCloseThrow.java:28)
     * 		at com.ww.java.java7.tryauto.AutoCloseThrow.main(AutoCloseThrow.java:14)
     *
     * 	try {}内异常不会丢失
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        try (FileReadAutoClose fileRead = new FileReadAutoClose()) {
            fileRead.read();
        }
    }
}

class FileReadAutoClose implements AutoCloseable {

    public void read() throws IOException {
        System.out.println("资源读取");
        throw new IOException("读取异常");
    }

    @Override
    public void close() throws Exception {
        System.out.println("资源关闭");
        throw new IOException("关闭异常");
    }
}